﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace CMS.Controls
{
    public class MyComboBox : ComboBox
    {

        public MyComboBox()
        {
            //SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            //SetStyle(ControlStyles.UserPaint, true);
            //SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            DrawMode = DrawMode.OwnerDrawFixed;
            FlatStyle = FlatStyle.Flat;
        }


        private readonly StringFormat _Format = new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center, FormatFlags = StringFormatFlags.NoWrap };

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

        }


        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);
            e.DrawFocusRectangle();
            if ((e.State & DrawItemState.Focus) != 0)
            {
                e.Graphics.FillRectangle(Brushes.SkyBlue, e.Bounds);
            }
            else
            {
                e.Graphics.FillRectangle(Brushes.White, e.Bounds);
            }
            if (e.Index != -1)
            {
                e.Graphics.DrawString(GetItemText(Items[e.Index]), Font, Brushes.Black, e.Bounds, _Format);
            }
        }

        protected override void WndProc(ref Message m)
        {//TextBox是由系统进程绘制，重载OnPaint方法将不起作用
            base.WndProc(ref m);
            if (m.Msg == 0xf)
            {
                Graphics g = CreateGraphics();
                //g.SmoothingMode = SmoothingMode.AntiAlias;
                //g.FillRectangle(Brushes.White, 0, 0, Width, Height);
                g.DrawRectangle(Pens.LightGray, 0, 0, Width - 1, Height - 1);
                //if (DropDownStyle == ComboBoxStyle.DropDownList)
                //{
                //    g.DrawString(Text, Font, new SolidBrush(ForeColor), new Rectangle(2, 2, Width - Height - 4, Height - 4), _Format);
                //}

                //if (DroppedDown)
                //{
                //    g.DrawImage(Resource1.ArrowTop16, Width - Height + (Height - 16) / 2 + 1, (Height - 16) / 2);
                //}
                //else
                //{
                //    g.DrawImage(Resource1.ArrowBottom16, Width - Height + (Height - 16) / 2 + 1, (Height - 16) / 2);
                //}
                //if (!Enabled)
                //{
                //    //g.FillRectangle(new SolidBrush(Color.FromArgb(40, 0, 0, 0)), new Rectangle(Point.Empty, Size));
                //}
                g.Dispose();
                m.Result = IntPtr.Zero;
            }


        }
    }
}
